package de.fhd.pms.ctrl;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.fhd.pms.dao.AutoDao;
import de.fhd.pms.dao.FahrtDao;
import de.fhd.pms.dao.KundeDao;
import de.fhd.pms.dao.StationDao;
import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Fahrt;
import de.fhd.pms.model.Kunde;

/**
 * The Controller class for {@link Fahrt} objects. Its tasks:
 * <ul>
 * <li>read and verify the parameters</li>
 * <li>access to the objects of the system</li>
 * <li>selection of the view and provision of content for the view</li>
 * </ul>
 * 
 * @author Josef Fischer
 * @author Alexander Neetzel
 * @author Philipp Six
 * 
 */
@Controller
public class FahrtController {
	private FahrtDao fahrtDao = null;
	private KundeDao kundeDao = null;
	private AutoDao autoDao = null;
	private StationDao stationDao = null;

	public void setFahrtDao(FahrtDao fahrtDao) {
		this.fahrtDao = fahrtDao;
	}

	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;
	}

	public void setAutoDao(AutoDao autoDao) {
		this.autoDao = autoDao;
	}

	public void setStationDao(StationDao stationDao) {
		this.stationDao = stationDao;
	}

	/**
	 * <p>
	 * Searches for all finished {@link Fahrt} objects and returns them in a
	 * <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/FinishedFahrt.html'.
	 * </p>
	 * 
	 * @return View name 'admin-list-fahrt-finished' selecting view
	 *         '/view/admin-list-fahrt-finished.jsp'.
	 */
	@RequestMapping(value = "/FinishedFahrt.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Alle abgeschlossene Fahrten");
		mv.addObject("message",
				"Hier sehen Sie alle Fahrten, die bereits durchgeführt wurden.");
		mv.addObject("fahrten", fahrtDao.findFinished());
		mv.setViewName("admin-list-fahrt-finished");
		return mv;
	}

	/**
	 * <p>
	 * Creates a new {@link Fahrt} object in dependency to a specific
	 * {@link Auto} and {@link Kunde} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/BeginFahrt.html'.
	 * </p>
	 * 
	 * @param id
	 * @return View name 'kunde-set-fahrt' selecting view
	 *         '/view/kunde-set-fahrt.jsp'.
	 */
	@RequestMapping(value = "/BeginFahrt.html", method = RequestMethod.GET)
	public ModelAndView beginneFahrt(@RequestParam(required = true) Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Eine Fahrt beginnen");
		mv.addObject(
				"message",
				"Wenn Sie mit diesem Auto eine Fahrt antreten möchten, dann wählen Sie bitte Ihren Namen aus.");
		mv.addObject("auto", autoDao.findById(id));
		mv.addObject("kunde", kundeDao.findAll());
		mv.addObject("google", stationDao.getGoogleCar(id));
		mv.addObject("Standort", "Standort des Autos:");
		mv.setViewName("kunde-set-fahrt");
		return mv;
	}

	/**
	 * <p>
	 * Saves the new {@link Fahrt} object in dependency to a specific
	 * {@link Auto} and {@link Kunde} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveFahrt.html'.
	 * </p>
	 * 
	 * @param auto
	 * @param kunde
	 * @return Redirect to 'SuccessFahrt.html'.
	 */
	@RequestMapping(value = "/SaveFahrt.html", method = RequestMethod.POST)
	public String save(int auto, int kunde) {
		fahrtDao.begin(auto, kunde);
		return "redirect:SuccessFahrt.html";
	}

	/**
	 * <p>
	 * Gives the customer the information that the {@link Fahrt} object was set
	 * successfully.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/SuccessFahrt.html'.
	 * </p>
	 * 
	 * @return View name 'kunde-set-fahrt-success' selecting view
	 *         '/view/kunde-set-fahrt-success.jsp'.
	 */
	@RequestMapping(value = "/SuccessFahrt.html", method = RequestMethod.GET)
	public ModelAndView success() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Erfolgreich!");
		mv.addObject("message",
				"Ihre Fahrt wurde erfolgreich angelegt. Sie können ab sofort das Auto nutzen.");
		mv.setViewName("kunde-set-fahrt-success");
		return mv;
	}

	/**
	 * <p>
	 * Searches for {@link Fahrt} objects that are not yet terminated and
	 * returns them in a <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/CurrentFahrt.html'.
	 * </p>
	 * 
	 * @return View name 'admin-list-fahrt-unfinished' selecting view
	 *         '/view/admin-list-fahrt-unfinished.jsp'.
	 */
	@RequestMapping(value = "/CurrentFahrt.html", method = RequestMethod.GET)
	public ModelAndView findCurrent() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Alle aktuellen Fahrten");
		mv.addObject(
				"message",
				"Hier sehen sie alle aktuellen Fahrten. Klicken Sie auf ein Auto um die Fahrt zu beenden.");
		mv.addObject("fahrten", fahrtDao.findUnfinished());
		mv.setViewName("admin-list-fahrt-unfinished");
		return mv;
	}

	/**
	 * <p>
	 * Terminates a {@link Fahrt} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/StopFahrt.html'.
	 * </p>
	 * 
	 * @param stationId
	 * @return Redirect to '/FinishedFahrt.html'
	 */
	@RequestMapping(value = "/StopFahrt.html", method = RequestMethod.GET)
	public String stop(@RequestParam(required = true) Integer stationId,
			int fahrtId, float km) {
		fahrtDao.end(fahrtId, stationId, km);
		return "redirect:FinishedFahrt.html";
	}

	/**
	 * <p>
	 * Shows the {@link Fahrt} object, which can be terminated.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/EndFahrt.html'.
	 * </p>
	 * 
	 * @param id
	 * @return View name 'admin-end-fahrt' selecting view
	 *         '/view/admin-end-fahrt.jsp'.
	 */
	@RequestMapping(value = "/EndFahrt.html", method = RequestMethod.GET)
	public ModelAndView end(@RequestParam(required = true) Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Aktuelle Fahrt beenden");
		mv.addObject("message",
				"Klicken Sie auf ein Auto um die Fahrt zu beenden.");
		mv.addObject("fahrt", fahrtDao.findById(id));
		mv.addObject("station", stationDao.findAll());
		mv.addObject("rechnung", fahrtDao.getInvoice(fahrtDao.findById(id)));
		mv.setViewName("admin-end-fahrt");
		return mv;
	}

	/**
	 * 
	 * Creates a {@link Rechnung} object when a specific {@link Fahrt} object is
	 * finished.
	 * 
	 * @param id
	 * @return mv
	 * 
	 * @author Josef Fischer
	 * @author Alexander Neetzel
	 * @author Philipp Six
	 */
	@RequestMapping(value = "/GetInvoice.html", method = RequestMethod.GET)
	public ModelAndView getInvoice(@RequestParam(required = true) Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Rechnung wird generiert");
		mv.addObject(
				"message",
				"Bitte warten Sie einen Moment, die Rechnung wird für die ausgewählte Fahrt erstellt.");
		mv.addObject("rechnung", fahrtDao.getInvoice(fahrtDao.findById(id)));
		mv.setViewName("admin-get-invoice");
		return mv;
	}
}
